package com.muli.muwai.controller;

import com.muli.muwai.constant.BankCashConstant;
import com.muli.muwai.constant.RetailConstant;
import com.muli.muwai.entity.*;
import com.muli.muwai.service.FinancingService;
import com.muli.muwai.service.ReturnMoneyService;
import com.muli.muwai.util.ExpAndImpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private FinancingService financingService;

    @Autowired
    private ReturnMoneyService returnMoneyService;


    @PostMapping("/financing")
    public String upload1(MultipartFile file) {
        List<FinancingExcel> importlist = ExpAndImpUtil.importExcel(file, 1, 1, FinancingExcel.class);
        // 枚举字段转化
        List<Financing> list = new ArrayList<Financing>();
        for (FinancingExcel entity : importlist) {
            Financing financing = new Financing();
            BeanUtils.copyProperties(entity, financing);
            if (entity.getRetail().contains(RetailConstant.hall.getName())) {
                financing.setRetail(RetailConstant.hall.getCode());
            }
            if (entity.getRetail().contains(RetailConstant.sale.getName())) {
                financing.setRetail(RetailConstant.sale.getCode());
            }
            if (entity.getRetail().contains(RetailConstant.other.getName())) {
                financing.setRetail(RetailConstant.other.getCode());
            }
            list.add(financing);
        }
        financingService.insertAll(list);
        return "success";
    }







}
